package vip.geeker.iotms.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import vip.geeker.iotms.dto.UserDto;
import vip.geeker.iotms.help.Constants;
import vip.geeker.iotms.persistance.model.DataSplitRule;
import vip.geeker.iotms.persistance.model.DeviceWarnInfo;
import vip.geeker.iotms.persistance.model.WarnRule;
import vip.geeker.iotms.service.impl.WarnRuleService;
import vip.geeker.iotms.service.impl.WarnService;
import vip.geeker.iotms.utils.ObjectUtils;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

@Controller
@RequestMapping("/warn/")
public class WarnController extends BaseController {

    @Resource
    private WarnRuleService warnRuleService;
    @Resource
    private WarnService warnService;

    @GetMapping("rule")
    public String toWarnRulePage() {
        return Constants.WARN_RULE;
    }


    @GetMapping("info")
    public String toWarnInfo() {
        return Constants.WARN_INFO;
    }


    @GetMapping("rule/page")
    @ResponseBody
    public Object queryWarnRulePage(WarnRule rule) {
        return warnRuleService.queryPage(rule);
    }

    @GetMapping("info/page")
    @ResponseBody
    public Object queryWarnPage(DeviceWarnInfo warn) {
        return warnService.queryPage(warn);
    }

    @GetMapping("/addWanRule")
    public String toAddWanRule(HttpServletRequest request, Integer id) {
        if(ObjectUtils.isNotNullAndEmpty(id)){
            // WarnRule splitRule = warnRuleService.queryById(id);
            // request.setAttribute("warnRule",splitRule);
        }else {
            request.setAttribute("warnRule",new WarnRule());
        }
        return Constants.WARN_RULE_ADD;
    }

    @PostMapping("/add")
    @ResponseBody
    public Object toAddWanRule(HttpServletRequest request, WarnRule rule) {
       // rule.setCreator(((UserDto) request.getSession().getAttribute(Constants.USER_SESSION_KEY)).getId());
        return warnService.toAddWanRule(rule);
    }

    @PostMapping("/delete")
    @ResponseBody
    public Object toDeleteWanRule(WarnRule rule) {
        return warnService.toDeleteWanRule(rule);
    }
}
